<?php
// +--------------------------------------------------------------------------
// | ISDCE [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2017 载攸道 版权所有 <TEL:086-010-57209502>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * [ 后台模块 ]
 */
namespace Admin\Controller;
use Common\Controller\AdminController;

/**
 * 菜单管理
 */
class MenuController extends AdminController{
	
	/**
	 * 菜单管理
	 */
    public function Manage(){
		
		action_log('admin_menu_manage');//行为记录 
		
		//页面配置
		$page = array(
			'title'=>'菜单管理',
			'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'author'=>'YiPHP',
			'robots' => 'all',
		
		);
		
		$this->assign('page',$page);
		
		
		$m_common_menu = M('common_menu');
		
		//获得上级信息
		$fmap['id'] = I('param.fmid')?I('param.fmid'):0;
		$data = $m_common_menu->where($fmap)->find();
		
		//获得菜单信息
		$map['fmid'] = $fmap['id'];
		$map['status'] = 1;
		
		$list = $m_common_menu->where($map)->order('sort desc')->select();
		
		//赋值
		$this->assign('tier',$data['tier']+1);
		$this->assign('list',$list);
		$this->assign('data',$data);
		$this->assign('fmid',$fmap['id']);
		
		$this->display();
	}
	
	/**
	 * API显示
	 */
    public function Api(){
		
		//页面配置
		$page = array(
			
			'title'=>'菜单信息',
			'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'author'=>'YiPHP',
			'robots' => 'all',
		
		);
		
		$this->assign('page',$page);
		
		
		$m_common_menu = M('common_menu');
		
		//获得上级信息
		$map_top['id'] = I('param.id');
		$map_top['status'] = 1;
		$data = $m_common_menu->where($map_top)->find();
		
		//获得菜单信息
		$map['fmid'] = $map_top['id'];
		$map['status'] = 1;
		
		$list = $m_common_menu->where($map)->order('sort desc')->select();
		
		foreach($list as $i=>$k){
			
			$down_map['fmid'] = $list[$i]['id'];
			$down_map['status'] = 1;
		
			$list[$i]['down'] = $m_common_menu->where($down_map)->order('sort desc')->select();
		
			
		}
		
		$data['down'] = $list;
		
		$this->assign('data',$data);
		$this->assign('url',getWebsiteURL());
		
		$this->display();
	}
	
	/**
	 * 新增菜单
	 */
	public function Create(){
		
		action_log('admin_menu_create');//行为记录
		
		//判断是否是ajax请求
		if(IS_AJAX){

			$m_common_menu = M('common_menu');
			
			//获得数据
			$data = I();
			
			$data['status'] = 1;
			
			$result = $m_common_menu->add($data);
			
			//返回信息
			$info = $result?get_errcode(0):get_errcode(-1);
				
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);
	}
	
	/**
	 * 编辑菜单
	 */
	public function Edit(){
    
		action_log('admin_menu_edit');//行为记录
		
		//判断是否是ajax请求
		if(IS_AJAX){

			$m_common_menu = M('common_menu');
			
			$map['id'] = I('param.id');
			
			$data = I('');
			unset($data['id']);
		
			$result = $m_common_menu->where($map)->save($data);
			
			//返回信息
			$info = $result?get_errcode(0):get_errcode(-1);
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);
	}
	
	/**
	 * 批量删除菜单
	 */
	public function Delete($id,$status=0){
		
		action_log('admin_menu_delete');//行为记录
		
		//判断是否是ajax请求
		if(IS_AJAX){
				
			$m_common_menu = M('common_menu');
			
			$id = $id?$id:I('param.id');
			$status = $status?$status:I('param.status');
			
			if($id){
				
			
				$id = explode(',',$id); 
				
				//连带下级删除
				foreach($id as $i=>$k){
					
					$map['fmid'] = $id[$i];
					
					$m_common_menu->delete($id[$i]);
					$result = $m_common_menu->where($map)->delete();
					
					
				}
				
				//返回信息
				$info = get_errcode(0);
			
			}else{
				
				$info = get_errcode(10000001);
			}
			
	
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);


	}
	
	
}